
import {getGameTypeList,delGameList,release,generateQrCode,activityCancel} from './gameConfigListServ.js'
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import { message } from 'antd';

const defultState = {
    gameTypeList:[],
    type:'',
    biz:{

    },
    zmzjCode:'',
    zysqCode:'',
    showModal:false,
    TypeModalFlag:false,
    imgLoading:false
}

// 声明module
const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    //   dispatch({type: 'queryInfo', payload: {}}) // end dispatch
                    dispatch({ type: 'getGameTypeList', payload: {...query} })
                    dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
                    // dispatch({ type: 'updateStore', payload: {gameActivityTypeId: query.gameActivityTypeId} });
                }
            })
        },
    },


    // 方法列表
    effects: {
        *getGameTypeList({ payload }, { put, call, select }) {
            try {
            let {params} = payload
            let {success,result} = yield call(getGameTypeList, {...params })
            // debugger
            if (success) {
                let gameTypeList = result.records
                gameTypeList.forEach(item=>{
                    item.label = item.gameName
                    item.value = item.id
                })
                yield put({ type: 'updateStore', payload: { gameTypeList } })
            }
            }catch(e){
                console.log('')
            }
        },
        *delGameList({ payload }, { put, call, select }) {
            try{
                if(!payload.id){
                    message.warning('没有活动id，无法删除');
                    return
                }
                let {success,records} = yield call(delGameList, {id:payload.id })
                if(success){
                    let defultState = yield select(state => state[tmpModule.namespace])
                    message.success('删除成功');
                    defultState.biz.refreshListDatas(true)
                }else{
                    message.warning('删除失败');
                }
                // debugger
            }catch(e){

            }
        },
        *activityCancel({ payload }, { put, call, select }) {
            try {
            let {success,records} = yield call(activityCancel, {gameActivityId:payload.id })
            if(success){
                let defultState = yield select(state => state[tmpModule.namespace])
                message.success('取消发布成功');
                defultState.biz.refreshListDatas(true)
            }else{
                message.warning('取消发布失败');
            }
            }catch(e){
                
            }
        },
        *release({ payload }, { put, call, select }) {
            try {
                // debugger
                let {success,records} = yield call(release, {gameActivityId:payload.id })
                if(success){
                    let defultState = yield select(state => state[tmpModule.namespace])
                    message.success('发布成功');
                    defultState.biz.refreshListDatas(true)
                }else{
                    message.warning('发布失败');
                }
                // debugger
            }catch(e){

            }
        },
        *generateQrCode({ payload }, { put, call, select }){
            try{
                // let params = {
                //         "id": "2c9485dc70cec3ce0170d1a6c6cf000a",
                //         // "channelType": 1,
                //         // "page": "subPackages3/pages/gameManage/gameIndex/gameIndex",
                //         "params":{
                //          "gameActivityId":"2c9485dc70cec3ce0170d1a6c6cf000a"
                //         }
                // }
                yield put({ type: 'updateStore', payload: { imgLoading:true} })
                let data1 = yield call(generateQrCode, {...payload,channelType:0,page:payload.zmzjPage})

                //let data2 = yield call(generateQrCode, {...payload,channelType:1,page:payload.zysqPage})
                if(data1.success){
                    if(data1.msg){
                        yield put({ type: 'updateStore', payload: { zmzjCode:data1.msg} })
                    }else{

                    }
                }
                // if(data2.success){
                //     if(data1.msg){
                //         yield put({ type: 'updateStore', payload: { zysqCode:data2.msg} })
                //     }else{

                //     }
                // }
                yield put({ type: 'updateStore', payload: { imgLoading:false} })

            }catch(e){

            }
        }
    },

    // reducers: {
    //   updateStore(preState, action) {
    //     return Object.assign({}, preState, action.payload)
    //   },
    // }
    reducers: {
        updateStore(preState, action) {
            return { ...preState, ...action.payload };
        }
    }
}

export default tmpModule
